package com.xjrsoft.common.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.hutool.core.util.BooleanUtil;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @Author: tzx
 * @Date: 2022/5/7 10:13
 */
public class XjrSaAnnotationInterceptor implements HandlerInterceptor {
    public XjrSaAnnotationInterceptor() {
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String isWorkFlow = request.getHeader("isWorkFlow");
        //判断是否未工作流调用 如果是工作流调用默认全放行
        if (BooleanUtil.toBoolean(isWorkFlow) || SecureUtil.isAdminUser()) {
            return true;
        }

        if (!(handler instanceof HandlerMethod)) {
            return true;
        } else {
            Method method = ((HandlerMethod) handler).getMethod();
            SaStrategy.me.checkMethodAnnotation.accept(method);
            return true;
        }
    }
}